package com.dsl.dslsuzhou.controller;

import com.dsl.dslsuzhou.model.dto.DeviceQueryRequest;
import com.dsl.dslsuzhou.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 设备管理控制器
 */
@RestController
@RequestMapping("/api/${api-module}/devices")
@Slf4j
public class DeviceController {
    
    @Autowired
    private DeviceService deviceService;
    
    /**
     * 根据条件查询设备ID集合
     * GET /devices/ids?category=空调&area=东区&building=A栋&floor=3F
     */
    @GetMapping("/ids")
    public List<String> getDeviceIds(
            @RequestParam String category,
            @RequestParam(required = false) String area,
            @RequestParam(required = false) String building,
            @RequestParam(required = false) String floor) {
        
        try {
            // 构建请求参数
            DeviceQueryRequest request = new DeviceQueryRequest();
            request.setCategory(category);
            request.setArea(area);
            request.setBuilding(building);
            request.setFloor(floor);
            
            log.info("接收设备ID查询请求: category={}, area={}, building={}, floor={}", 
                    category, area, building, floor);
            
            // 调用服务层查询
            List<String> deviceIds = deviceService.getDeviceIds(request);
            
            log.info("返回设备ID数量: {}", deviceIds.size());
            
            return deviceIds;
            
        } catch (IllegalArgumentException e) {
            log.error("参数错误: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("查询设备ID失败", e);
            throw new RuntimeException("查询设备ID失败: " + e.getMessage());
        }
    }
}
